package com.boco.pciv.service.project.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.boco.pciv.SysProperties;
import com.boco.pciv.core.entity.SearchCriteria;
import com.boco.pciv.entity.page.*;
import com.boco.pciv.entity.project.CompanyProjectInfo;
import com.boco.pciv.mapper.project.IPageMapper;
import com.boco.pciv.service.project.IPageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class PageServiceImpl implements IPageService {

    @Resource
    IPageMapper iPageMapper;
    @Autowired
    SysProperties sysConfig;

    
    //劳务公司 项目块 数据处理
    public List<CompanyProjectInfo> getProjectListInfo(SearchCriteria param){
        Map<String,Object> map=new HashMap<>();
        map.put("companyid",param.getQueryMap().get("companyid"));
        List<CompanyProjectInfo> list=iPageMapper.getProList(map);
        Set<String> sPro=new HashSet<>();
        List<CompanyProjectInfo> newlist=new ArrayList<>();
        for(CompanyProjectInfo info:list){
            if(sPro.contains(info.getProjectId())){
                continue;
            }
            else{
                sPro.add(info.getProjectId());
            }
            map.put("projectid",info.getProjectId());
            String personNum=iPageMapper.getPersonNum(map);
            if(StrUtil.isBlank(personNum)) info.setPersonNum(0);
            else info.setPersonNum(Integer.parseInt(personNum));

            String yfPay=iPageMapper.getYFPay(map);
            if(StrUtil.isBlank(yfPay))info.setYfPay(0);
            else info.setYfPay(Float.parseFloat(yfPay));
            String sfPay=iPageMapper.getSFPay(map);
            if(StrUtil.isBlank(sfPay))info.setSfPay(0);
            else info.setSfPay(Float.parseFloat(sfPay));
            newlist.add(info);
        }
        return newlist;
    }

    //劳务公司 曲线图 数据处理
    public resultLine getproLineInfo(String companyid){
        Map<String,Object> map=new HashMap<>();
        map.put("companyid",companyid);
        List<CompanyProjectInfo> list=iPageMapper.getProList(map);
        resultLine line=new resultLine();
        line.xAxis=new ArrayList<>();
        line.data=new ArrayList<>();
        Date now= DateUtil.date();
        int num=Integer.parseInt(sysConfig.getTotalTime());
        while (num>0){
            line.xAxis.add(DateUtil.format(now,"yyyyMM"));
            now=DateUtil.offsetMonth(now,-1);
            num--;
        }
        Collections.sort(line.xAxis);

        Set<String> sPro=new HashSet<>();
        for (CompanyProjectInfo info : list) {
            if (sPro.contains(info.getProjectName())) {
                continue;
            } else {
                sPro.add(info.getProjectName());
            }

            echartsLine eline = new echartsLine();
            eline.data = new ArrayList<>();
            eline.name = info.getProjectName();
            eline.type = "line";
            map.put("projectid", info.getProjectId());
//            map.put("companyid",info.getCompanyId());
            List<payInfo> lpay = iPageMapper.getSfPayList(map);
            for (String t : line.xAxis) {
                List<payInfo> d = lpay.parallelStream()
                        .filter(x -> x.getTime().equals(t))
                        .collect(Collectors.toList());
                if (d.size() == 0) {
                    eline.data.add("0");
                } else {
                    eline.data.add(d.get(0).getPay());
                }
            }
            long c=eline.data.stream().filter(x->x.equals("0")).count();
            if(c==eline.data.size())continue;
            line.data.add(eline);
        }
        line.legend=new ArrayList<>(sPro);
        return line;
    }

    //劳务公司 饼图 数据处理
    public resultPie getproPieInfo(String companyid){
        Map<String,Object> map=new HashMap<>();
        map.put("companyid",companyid);
        List<CompanyProjectInfo> list=iPageMapper.getProList(map);
        resultPie pie=new resultPie();
        pie.data=new ArrayList<>();
        pie.legend=list.stream().map(x->x.getProjectName()).collect(Collectors.toList());
        Set<String> sPro=new HashSet<>();
        for(CompanyProjectInfo info:list){
            if(sPro.contains(info.getProjectId())){
                continue;
            }
            else{
                sPro.add(info.getProjectId());
            }
            echartsPie epie=new echartsPie();
            epie.name=info.getProjectName();
            map.put("projectid",info.getProjectId());
            String sfPay=iPageMapper.getSFPay(map);
            if(StrUtil.isBlank(sfPay)){
                continue;
            }
            else{
                epie.value=Float.parseFloat(sfPay);
            }
            pie.data.add(epie);
        }
        pie.data=pie.data.stream().distinct().collect(Collectors.toList());
        return pie;
    }

    //首页项目块 数据处理
    public CompanyProjectInfo getproinfo(String companyid){
        CompanyProjectInfo info=new CompanyProjectInfo();
        Map<String,Object> map=new HashMap<>();
        map.put("companyid",companyid);
        map.put("status","在建");
        String zj=iPageMapper.getZJCount(map);
        if(StrUtil.isBlank(zj))info.setZjNum(0);
        else info.setZjNum(Integer.parseInt(zj));
        map.put("status","竣工");
        String jg=iPageMapper.getZJCount(map);
        if(StrUtil.isBlank(jg))info.setZjNum(0);
        else info.setJgNum(Integer.parseInt(jg));
        //在职工人数
        String personNum=iPageMapper.getpersonNum(map);
        if(StrUtil.isBlank(personNum))info.setZzPersonNum(0);
        else info.setZzPersonNum(Integer.parseInt(personNum));
        //已发工资数
        String payNum=iPageMapper.getSFPay(map);
        if(StrUtil.isBlank(payNum))info.setPayNum(0);
        else info.setPayNum(Float.parseFloat(payNum));
        return info;
    }

    //首页 柱状图 数据处理
    public resultBar getBarData(String companyid){
        resultBar bar=new resultBar();
        bar.xAxis=new ArrayList<>();
        bar.yfData=new ArrayList<>();
        bar.sfData=new ArrayList<>();
        Date now = DateUtil.date();
        int num = Integer.parseInt(sysConfig.getTotalTime());
        while (num > 0) {
            bar.xAxis.add(DateUtil.format(now, "yyyyMM"));
            now = DateUtil.offsetMonth(now, -1);
            num--;
        }
        Collections.sort(bar.xAxis);

        Map<String,Object> map=new HashMap<>();
        map.put("companyid",companyid);
        List<payInfo> lpay=iPageMapper.getPayByTimeList(map);
        for(String t:bar.xAxis){
            List<payInfo> d=lpay.parallelStream()
                    .filter(x->x.getTime().equals(t))
                    .collect(Collectors.toList());
            if(d.size()==0){
                bar.yfData.add("0");
                bar.sfData.add("0");
            }
            else{
                bar.yfData.add(d.get(0).getExpectpay());
                bar.sfData.add(d.get(0).getPay());
            }
        }
        return bar;
    }
    //首页 柱状图R 数据处理
    public resultBar getBarRData(String companyid){
        resultBar bar=new resultBar();
        bar.xAxis=new ArrayList<>();
        bar.yfData=new ArrayList<>();
        bar.sfData=new ArrayList<>();
        Map<String,Object> map=new HashMap<>();
        map.put("companyid",companyid);
        List<CompanyProjectInfo> list=iPageMapper.getProList(map);
        bar.xAxis=list.stream().map(x->x.getProjectName()).distinct().collect(Collectors.toList());
        map.put("time",DateUtil.format(DateUtil.date(),"yyyyMM"));
        List<payInfo> lpay=iPageMapper.getPayByProjectList(map);
        for(String t:bar.xAxis){
            List<payInfo> d=lpay.parallelStream()
                    .filter(x->x.getTime().equals(t))
                    .collect(Collectors.toList());
            if(d.size()==0){
                bar.yfData.add("0");
                bar.sfData.add("0");
            }
            else{
                bar.yfData.add(d.get(0).getExpectpay());
                bar.sfData.add(d.get(0).getPay());
            }
        }
        return bar;
    }

}
